package export

import (
	// boot
	_ "gfrest/web/boot"
	"io/ioutil"
	"os"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gfsnotify"
	"github.com/gogf/gf/os/glog"
)

func init() {
	ExportConfigEnvFile()
	fpath, _ := g.Cfg().GetFilePath()
	_, err := gfsnotify.Add(fpath, func(event *gfsnotify.Event) {
		ExportConfigEnvFile()
	})

	if err != nil {
		glog.Error(err)
	}
}

// ExportConfigEnvFile 将配置设置到环境变量中
func ExportConfigEnvFile() {

	sh := "DB_ENV = {"
	sh = sh + "\n\"DB_HOST\": \"" + g.Config().GetString("database.default.0.host") + "\","
	sh = sh + "\n\"DB_PORT\": \"" + g.Config().GetString("database.default.0.port") + "\","
	sh = sh + "\n\"DB_USER\": \"" + g.Config().GetString("database.default.0.user") + "\","
	sh = sh + "\n\"DB_PASS\": \"" + g.Config().GetString("database.default.0.pass") + "\","
	sh = sh + "\n\"DB_NAME\": \"" + g.Config().GetString("database.default.0.name") + "\","
	sh = sh + "\n\"APP_NAME\": \"" + g.Config().GetString("appName") + "\","
	sh = sh + "\n}"
	d1 := []byte(sh)
	os.Mkdir("dbenv", 0700)
	println("ExportConfigEnvFile ", d1)

	err := ioutil.WriteFile("dbenv/__init__.py", d1, 0644)
	if err != nil {
		glog.Info(err)
	}
}
